/*
 *  Copyright (C) 2011 felixtheripper <felixtheripper_AT_gmail.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */


package org.myorg.ctm.etk.gui.swg.action.actionmanager;

import com.sun.jaf.ui.DelegateAction;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
import org.myorg.ctm.etk.gui.swg.action.actions.*;
import org.myorg.ctm.etk.gui.swg.action.actioncontroller.ActionController;


/**
 *
 * @author felixtheripper
 *
 * email: felixtheripper_AT_gmail.com
 * ... please remove "_AT_" from the above string to get the right email address
 *
 */

abstract public class ActionManagerPerspAbs extends ActionManagerAbs
{
    protected void unloadActions() {
        if (super._mapIdToAction_ != null) {
            super._mapIdToAction_.remove(ActGuiAbout.s_getInstance().getId());
            super._mapIdToAction_.remove(ActGuiProperties.s_getInstance().getId());
            super._mapIdToAction_.remove(ActGuiExit.s_getInstance().getId());
            super._mapIdToAction_.remove(ActGuiCloseSession.s_getInstance().getId());
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        this._acr = null;
    }

    protected ActionManagerPerspAbs(ActionController acr) throws Exception {
        super();
        this._acr = acr;
    }


    protected void loadActions() throws Exception
    {
        super._addAction_(ActGuiAbout.s_getInstance().getId(),
                ActGuiAbout.s_getInstance());

        super._addAction_(ActGuiProperties.s_getInstance().getId(),
                ActGuiProperties.s_getInstance());

        super._addAction_(ActGuiCloseSession.s_getInstance().getId(),
                ActGuiCloseSession.s_getInstance());

        super._addAction_(ActGuiExit.s_getInstance().getId(),
                ActGuiExit.s_getInstance());
    }
 
    protected void registerCallback() {

        this._registerCallback_(this._acr, ActGuiAbout.s_getInstance(),
                ActGuiAbout.s_getInstance().getIdShort());

        this._registerCallback_(this._acr, ActGuiProperties.s_getInstance(),
                ActGuiProperties.s_getInstance().getIdShort());

        this._registerCallback_(this._acr, ActGuiCloseSession.s_getInstance(),
                ActGuiCloseSession.s_getInstance().getIdShort());

        this._registerCallback_(this._acr, ActGuiExit.s_getInstance(),
                ActGuiExit.s_getInstance().getIdShort());
    }

    protected void _registerCallback_(ActionController acrHandler, ActGuiAbs act, String strIdShort)
    {
        Object id = act.getId();
        DelegateAction a = super.getDelegateAction(id);

        String strMethod = strIdShort + ActionManagerPerspAbs._STR_SUFFIX_METHOD_;

        if (a != null)
        {
	    // Create a new ActionListener using the dynamic proxy api.
	    a.addActionListener((ActionListener)EventHandler.create(ActionListener.class,
                   acrHandler, strMethod));
	}

        super.registerCallback(id, acrHandler, strMethod);
    }
    final static private String _STR_SUFFIX_METHOD_ = "Handler";
    protected ActionController _acr = null;
}
